package com.ming.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.yygh.common.result.Result;
import com.ming.yygh.common.utils.MD5;
import com.ming.yygh.hosp.service.HospitalSetService;
import com.ming.yygh.model.hosp.HospitalSet;
import com.ming.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Light
 * @date 2021/11/15 15:24
 */

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    @Resource
    private HospitalSetService hospitalSetService;

    // http:127.0.0.1:8201/admin/hosp/hospitalSet/findAll

    // 1、查询医院设置表里的所有信息
    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAllHospitalSet() {
        // 调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    // 2、通过id逻辑删除医院设置
    @ApiOperation(value = "通过id逻辑删除医院设置")
    @DeleteMapping("/{id}")
    public Result<Boolean> removeHospSet(@ApiParam(name = "id", value = "医院设置id", required = true)
                                         @PathVariable(value = "id") String id) {
        boolean success = hospitalSetService.removeById(id);
        if (success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 3、条件查询带分页
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/findPageHospitalSet/{current}/{limit}")
    public Result<Page<HospitalSet>> findPageHospSet(@PathVariable("current") Long current,
                                                     @PathVariable("limit") Long limit,
                                                     @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        // 创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);

        // 构造条件
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(hospitalSetQueryVo.getHosName())) {
            queryWrapper.like("hosname", hospitalSetQueryVo.getHosName());
        }
        if (StringUtils.isNotBlank(hospitalSetQueryVo.getHosCode())) {
            queryWrapper.eq("hoscode", hospitalSetQueryVo.getHosCode());
        }

        // 调用方法，实现分页的查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);
        return Result.ok(hospitalSetPage);

    }

    // 4、添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result<Boolean> saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // 设置状态; 1 使用 0 不使用
        hospitalSet.setStatus(1);
        // 签名密钥
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + ""));

        // 调用service
        boolean success = hospitalSetService.save(hospitalSet);
        if (success) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    // 5、根据id获取医院设置
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("/getHospitalSet/{id}")
    public Result<HospitalSet> getHospitalSet(@ApiParam(name = "id", value = "医院设置id", required = true)
                                              @PathVariable("id") Long id) {
//        try {
//            //模拟异常
//            int a = 1/0;
//        }catch (Exception e) {
//            throw new YyghException("失败",201);
//        }

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    // 6、修改医院设置
    @ApiOperation("修改医院设置")
    @PostMapping("/updateHospitalSet")
    public Result<Boolean> updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean success = hospitalSetService.updateById(hospitalSet);
        if (success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 7、批量删除医院设置
    @ApiOperation("批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public Result<Boolean> batchRemoveHospitalSet(@RequestBody List<Long> idLists) {
        boolean success = hospitalSetService.removeByIds(idLists);
        return Result.ok(success);
    }

    // 8、医院设置的锁定和解锁
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result<Boolean> lockHospitalSet(@PathVariable("id") Long id,
                                           @PathVariable Integer status) {
        // 根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 修改查询到的医院设置状态
        hospitalSet.setStatus(status);
        boolean success = hospitalSetService.updateById(hospitalSet);

        return Result.ok(success);
    }

    // 9、发送签名密钥
    @PutMapping("/sendKey/{id}")
    public Result<Boolean> sendKey(@PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        // TODO 发送短信
        return Result.ok();
    }

}
